from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired, Length, Email, Regexp
from app.libs.enums import ClientTypeEnum
from app.models.user import User
from wtforms import ValidationError
from app.validators.base import BaseForm as Form


class ClientForm(Form):  # 不同客户端的注册信息的共有属性
    account = StringField(validators=[DataRequired(message='不允许为空'), Length(min=5, max=32)])
    secret = StringField()
    type = IntegerField(validators=[DataRequired()])


    def validate_type(self, value):
        try:
            # 数字向枚举转换
            client = ClientTypeEnum(value.data)
        except ValueError as e:
            raise e
        self.type.data = client

class LoginGetTokenForm(ClientForm):  # 不同客户端的注册信息的共有属性
    account = StringField(validators=[DataRequired(message='不允许为空')])
    secret = StringField(validators=[DataRequired(message='不允许为空')])
    type = IntegerField(validators=[DataRequired()])
    def validate_account(self,value):
        if not User.query.filter_by(email=value.data).first():
            raise ValidationError(message='用户不存在')


class ArgsForm(Form):
    email=StringField(validators=[DataRequired(message='不允许为空')])
    nickname=StringField(validators=[DataRequired(message='不允许为空')])

class UserEmailForm(ClientForm):
    account = StringField(validators=[Email(message='invalidate email')])
    secret = StringField(validators=[DataRequired(),
                                     Regexp(r'^[A-Za-z0-9_*&$#@]{6,22}$')
                                     ])
    nickname = StringField(validators=[DataRequired(), Length(min=2, max=22)])

    def validate_account(self, value):
        if User.query.filter_by(email=value.data).first():
            raise ValidationError(message='该邮箱已被注册')

    def validate_nickname(self, value):
        if User.query.filter_by(nickname=value.data).first():
            raise ValidationError(message='该用户名已被使用')

class TokenForm(Form):
    token=StringField(validators=[DataRequired()])